package com.study.rabbitmq.controller;

import com.study.rabbitmq.mqtt.MqttProviderConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/producer")
public class MessageSendController {

    @Autowired
    private MqttProviderConfig mqttProviderConfig;

    /**
     * http://localhost:8325/study-two-rabbitmq/producer/sendMsg?qos=1&retained=false&topic=pubTopic&message=hello,lilei
     * 发送消息
     * @param qos
     * @param retained
     * @param topic
     * @param message
     * @return
     */
    @GetMapping("/sendMsg")
    public String sendMessage(int qos,boolean retained,String topic,String message){
        log.info("MessageSendController 发送消息， topic:{}, message:{}", topic, message);
        try {
            mqttProviderConfig.publish(qos, retained, topic, message);
            return "发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送消息失败：{}", e.getMessage());
            return "发送失败";
        }
    }
}
